package logics

import (
	"fmt"
	"venus/common"
	"venus/conf"
	"venus/servers/game/objects"
	"venus/servers/pub/pub_mgr"
	"venus/service/sv_tcp/znet/zpb"
	"venus/util/zredis"
	"venus/util/ztime"
)

/*
用户在不同服务器的Puid User:Puid:<Uuid> -- ConfigId -- Puid
*/

const RdsUuidPid = "user:puid"
const RdsNamePid = "player:namepid"

func SetUuidPid(uuid int64, pid int64) {
	tableName := fmt.Sprintf("%s:%d", RdsUuidPid, uuid)
	zredis.HSet(tableName, common.ServerId, pid)
}

func GetUuidPid(uuid int64) int64 {
	tableName := fmt.Sprintf("%s:%d", RdsUuidPid, uuid)
	return zredis.HGetInt64(tableName, common.ServerId)
}

func SetNamePid(name string, pid int64) {
	zredis.HSet(RdsNamePid, name, pid)
}

func GetNamePid(name string) int64 {
	return zredis.HGetInt64(RdsNamePid, name)
}

func CheckPlayerName(name string) conf.EProtocolResult {
	// 判断长度
	if len(name) < conf.GlobalGetInt(conf.EGlobalId昵称长度最小) {
		return conf.EProtocolResult名字长度太短
	}
	if len(name) > conf.GlobalGetInt(conf.EGlobalId昵称长度最大) {
		return conf.EProtocolResult名字长度太长
	}
	// 判断屏蔽词库
	if pub_mgr.CheckNameBad(name) {
		return conf.EProtocolResult名字含有不合法或者不雅字符
	}
	return 0
}

// PlayerCreate 玩家创建
func PlayerCreate(player *objects.Player, req *zpb.C2GPlayerCreate) {
	player.TimeRegister = ztime.UnixMilli()

	player.Name = req.Name

	player.Gold = conf.GlobalGetInt64(conf.EGlobalId玩家初始金币)
	player.Gem = conf.GlobalGetInt64(conf.EGlobalId玩家初始宝石)
	player.Stamina = conf.GlobalGetInt64(conf.EGlobalId玩家初始体力)
	player.Energy = conf.GlobalGetInt64(conf.EGlobalId玩家初始精力)
}
